/**
 * 查询辅助类
 * @class Selector
 * @author iwangcj
 */
class Selector {

    constructor(){

    }

    /**
     * 参考jquery的选择器函数，目前支持 "*"所有元素,"selected"所有选中的元素 ,
     * "#id",某个id元素 ,"[attribute]"包含attribute属性的所有元素,"[attribute=value]" 
     * ,"[attribute!=value]","[attribute$=value]",比如[userData!=],user-data不为空值的元素
     * 参见 http://www.w3school.com.cn/jquery/jquery_ref_selectors.asp
     * @param {string} selector 查询条件
     * @param {number} deep 查找的层次
     */
    select(parent, selector, deep=0) {
        let findedList = [];
        if (parent && selector && typeof selector === 'string') {
            const selstr = selector.replace(/(^\s*)|(\s*$)/g, "");
            if (selstr === '*') {
                findedList = this._selectAll(parent, "*", deep);
            }
            else if (selstr.indexOf("#") === 0) {
                const id = selector.replace(/#/g, "");  
                findedList = this._finderById(parent, "#", id);
            }
            else if (selstr.indexOf("[") === 0) {
                const leftIndex = selstr.indexOf("[");
                const rightIndex = selstr.indexOf("]");
                if (rightIndex > leftIndex) {
                    const data = selstr.substring(leftIndex+1, rightIndex);
                    findedList = this._finderByCondtion(parent, data);
                }
            }
        }

        return findedList;
    }

    /**
     * 根据属性查找元素
     * @param {string} data 
     * @param {number} deep 
     */
    _finderByCondtion(parent, data, deep) {
        const findedList = [];
        let condition = 'has';
        if (data.indexOf("!=") > -1) {
            condition = "!=";
        }
        else if (data.indexOf("$=") > -1) {
            condition = "$=";
        }
        else if (data.indexOf("=") > -1) {
            condition = "=";
        }
        else {
            condition = "has";
        }

        if (condition === 'has') {
            parent &&　parent.children.forEach((item)=>{
                const tempValue = item.attr(data);
                if (tempValue) {
                    findedList.push(item);
                }
                if (deep === 0) {
                    this._finder(item,  findedList, "[", condition, data, value);
                }
            });
        }
        else {
            let key = null, 
                value = null;
            const splitList = data.split(condition);
            if (splitList.length == 2) {
                key = splitList[0];
                value = splitList[1];
            }

            if (key && value) {
                parent &&　parent.children.forEach((item)=>{
                    const tempValue = item.attr(key);
                    if ((condition === '=' && tempValue && tempValue == value) ||
                        (condition === '!=' && tempValue && tempValue != value) ||
                        (condition === '$=' && tempValue && tempValue.toString().indexOf(value) > 0)) {
                            findedList.push(item);
                    }
                    if (deep === 0) {
                        this._finder(item,  findedList, "[", condition, key, value);
                    }
                });
            }
        }

        return findedList;
    }

    /**
     * 根据id查找图元
     * @param {string} id 
     * @param {number} deep 
     */
    _finderById(parent, id, deep=0) {
        const findedList = [];
        parent && parent.children.forEach((item)=>{
            if (item.id && item.id === id) {
                findedList.push(item);
            }
            if (deep === 0) {
                this._finder(item,  findedList, "#", null, null, id);
            }
        });
        return findedList;
    }

    /**
     * 私有函数，查找元素用
     * @param {Nice.Element} parent 父元素
     * @param {Array} findedList 已找到元素列表
     * @param {string} type * all [ 条件查询
     * @param {string} condition 条件
     * @param {string} key 关键字
     * @param {string} value 查找值
     */
    _finder(parent, findedList, type, condition, key, value) {        
        if(type === "*") {
            parent.forEach((item)=>{
                findedList.push(item);
                this._finder(item, findedList, type, selector);
            });
        }
        else if (type === "[") {
            parent.forEach((item)=>{
                const tempValue = item.attr(key);
                if ((condition === 'has' && tempValue) ||
                    (condition === '=' && tempValue && tempValue == value) ||
                    (condition === '!=' && tempValue && tempValue != value) ||
                    (condition === '$=' && tempValue && tempValue.toString().indexOf(value) > 0)) {
                        findedList.push(item);
                }
                this._finder(item, findedList, type, condition, key, value);
            });
        }
        else if (type === "#") {
            parent.forEach((item)=>{
                if (item.id && item.id === id) {
                    findedList.push(item);
                }
                this._finder(item, findedList, type, condition, key, value);
            });
        }
    }

    /**
     * 查找所有图元
     * @param {number} deep 查找的层次 0所有; 1 一层
     */
    _selectAll(parent, deep = 0) {
        const findedList = [];
        parent && parent.children.forEach((item)=>{
            findedList.push(item);
            if (deep === 0) {
                this._finder(item, findedList, "*", "*");
            }
        });
        return findedList;
    }
}

export default new Selector();